import React, { Component } from 'react';
import './Search.scss';
import eventBus from '../comment/bus';
class Search extends Component {
  constructor() {
    super();
    this.state = {
      username: '',
      value: '',
    };
  }
  inputChange = (e) => {
    console.log(e.target.value);
    // let val = this.refs.username.value;
    this.setState({
      username: e.target.value,
    });
  };
  getInputValue = () => {
    eventBus.emit('comment', this.state.username);
  };
  onSubmit = (e) => {
    //阻止默认事件
    //preventDefault()[dom标准写法(ie678不兼容)]
    //ie678用returnValue
    //或者利用return false也能阻止默认行为,没有兼容问题(只限传统注册方式)
    e.preventDefault();
  };
  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <div className="search">
          <input
            type="text"
            defaultValue={this.state.value}
            onChange={this.inputChange}
          />
          <button onClick={() => this.getInputValue()}>百度一下</button>
        </div>
      </form>
    );
  }
}

export default Search;
